import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.filechooser.FileSystemView;

import sun.awt.shell.ShellFolder;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author chengzepeng
 * @date 2023/04/20 15:53
 */
public class GetSystemIcon {

    /**
     * @param args
     */
    public static void main(String[] args) throws FileNotFoundException {

//        GetSystemIcon.getIcon1();
//        GetSystemIcon.getIcon2(args[0]);
        GetSystemIcon.getIcon2(args[0], args[1], args[2]);

//        System.out.println("Your first argument is: " + args[0]);
    }

    /**
     * 参考网址：https://blog.csdn.net/weixin_34023863/article/details/91560836
     * 第一种形式获取icon,利用javax.swing中FileSystemView获取
     * 缺点就是很小，很模糊
     */
    public static void getIcon1() {
        // --------------------------------------------
        // 缺点就是很小，很模糊
        File file1 = new File("C:\\Program Files (x86)\\KuGou2012\\KuGou.exe");
        Image image = ((ImageIcon) FileSystemView.getFileSystemView().getSystemIcon(file1)).getImage();
        int width = 10;
        int height = 10;
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        File f = new File("D:\\KuGou.exe11.png");
        try {
            ImageIO.write(bi, "png", f);
        } catch (IOException e) {
//            log.error("写png文件失败", e);
            System.out.println("写png文件失败");
        }
    }

    /**
     * 推荐使用
     * 参考网址：https://zhidao.baidu.com/question/317417746.html
     * 第二种形式获取icon,利用通过awt.shellFolder获取图标
     * 获取的32*32 比较清晰，可以使用
     */
    public static void getIcon2(String path, String outPath, String name) throws FileNotFoundException {
//        File file = new File("C:\\Program Files (x86)\\DTLSoft\\DriveTheLife\\DriveTheLife.exe");
        File file = new File(path);
        System.out.println(path.lastIndexOf("D"));
        System.out.println(path.substring(path.lastIndexOf("\\") + 1));
        // 图标保存地址
        OutputStream inStream = new FileOutputStream(new File(outPath + name + ".png"));
//        OutputStream inStream = new FileOutputStream(new File("./QQScLauncher.exe_64.png"));

        try {
            // 通过awt.shellFolder获取图标 默认为32 *32
            ShellFolder shellFolder = ShellFolder.getShellFolder(file);
            ImageIcon icon = new ImageIcon(shellFolder.getIcon(true));
            BufferedImage imgIcon = (BufferedImage) icon.getImage();

            // 调整icon图标大小，放大后会模糊
            imgIcon = resize(imgIcon, 32, 32);

            ImageIO.write(imgIcon, "png", inStream);
            inStream.flush();
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调整大小
     *
     * @param img
     * @param newW
     * @param newH
     * @return
     */
    public static BufferedImage resize(BufferedImage img, int newW, int newH) {
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
        Graphics2D g = dimg.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
        g.dispose();
        return dimg;
    }

}